﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PDS2
{
    class ParticleEngine
    {
        List<Particle> particles = new List<Particle>();

        public ParticleEngine()
        {
        }

        public void Update(GameTime gameTime)
        {
            for (int i = 0; i < particles.Count; i++)
            {
                particles[i].Update(gameTime);
                if (particles[i].Dead)
                    particles.RemoveAt(i);
            }
        }

        public void AddParticle(Particle particle)
        {
            particles.Add(particle);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Particle particle in particles)
            {
                particle.Draw(spriteBatch);
            }
        }
    }
    public class Particle
    {
        public Vector2 Position;
        public Texture2D Texture;
        public Color Color;
        public float Scale;
        public float Rotation;

        public float TimeAlive;
        public float FadeTime;

        public float Velocity;
        public Vector2 Direction;
        public bool Dead = false;

        public Particle(Texture2D tex, Vector2 pos, Color color, float rot, float scale, float velocity, Vector2 direction, float fadeTime)
        {
            this.Texture = tex;
            this.Position = pos;
            this.Color = color;
            this.Rotation = rot;
            this.Scale = scale;
            this.Velocity = velocity;
            this.Direction = direction;
            this.FadeTime = fadeTime;
        }

        public void Update(GameTime gameTime)
        {
            TimeAlive += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (TimeAlive > FadeTime)
            {
                Dead = true;
            }

            Position += Direction * Velocity;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, Position, null, Color, Rotation, new Vector2(Texture.Width / 2, Texture.Height / 2), Scale, SpriteEffects.None, 0.001f);
        }
    }
}
